var prefix = "/noCheck/manage";
$(function () {
    load();
});
$('#noCheckCommandersTable').on('load-success.bs.table', function (e, data) {
    if (data.total && !data.rows.length) {
        $('#noCheckCommandersTable').bootstrapTable('selectPage').bootstrapTable('refresh');
    }
});

function load() {
    $('#noCheckCommandersTable').bootstrapTable(
            {
                method: 'get', // 服务器数据的请求方式 get or post
                url: prefix + "/list", // 服务器数据的加载地址
                showRefresh: true,
                // showToggle : true,
                iconSize: 'outline',
                // toolbar: '#exampleToolbar',
                striped: true, // 设置为true会有隔行变色效果
                dataType: "json", // 服务器返回的数据类型
                pagination: true, // 设置为true会在底部显示分页条
                pageList:[10,20,50],
                // queryParamsType : "limit",
                // //设置为limit则会发送符合RESTFull格式的参数
                singleSelect: false, // 设置为true将禁止多选
                // contentType : "application/x-www-form-urlencoded",
                // //发送到服务器的数据编码类型
                pageSize: 10, // 如果设置了分页，每页数据条数
                pageNumber: 1, // 如果设置了分布，首页页码
                // search : true, // 是否显示搜索框
                showColumns: true, // 是否显示内容下拉框（选择显示的列）
                sidePagination: "server", // 设置在哪里进行分页，可选值为"client" 或者
                // "server"
                queryParams: function (params) {
                    return {
                        limit: params.limit,
                        offset: params.offset,
                        order: 'desc',
                        startDate: $.trim($("#startDate").val()),
                        endDate: $.trim($("#endDate").val()),
                        teamNum: $.trim($("#teamNum").val()),
                        realname: $.trim($('#realname').val()),
                        nickname: $.trim($('#nickname').val()),
                        mobile: $.trim($('#mobile').val()),
                        centreId: $('#centreId').val()
                    };
                },
                // //请求服务器数据时，你可以通过重写参数的方式添加一些额外的参数，例如 toolbar 中的参数 如果
                // queryParamsType = 'limit' ,返回参数必须包含
                // limit, offset, search, sort, order 否则, 需要包含:
                // pageSize, pageNumber, searchText, sortName,
                // sortOrder.
                // 返回false将会终止请求
                columns: [
                    {
                        checkbox: true
                    },
                    {
                    	field: 'id',
                        title: '用户名[ID]',
                        formatter: function (value, row, index) {
                        	if(row.realname != '' && row.realname != null){
                        		return row.realname + '[' + row.id + ']';
                        	}else{
                        		if(row.nickname != '' && row.nickname != null){
                        			return row.nickname + '[' + row.id + ']';
                        		}else{
                        			return '[' + row.id + ']';
                        		}
                        	}
                        }
                    },
                    {
                        field: 'gender',
                        title: '性别',
                        formatter: function (value, row, index) {
                        	if(row.gender == 1){
                        		return '男';
                        	}
                        	if(row.gender == 2){
                        		return '女';
                        	}
                        }
                    },
                    {
                        field: 'mobile',
                        title: '手机号码'
                    },
                    {
                        field: 'upTeamname',
                        title: '门店推荐人'
                    },
                    {
                        field: 'bondmoney',
                        title: '保证金'
                    },
                    {
                        field: 'shimingzhi',
                        title: '实名认证',
                        formatter: function (value, row, index) {
                        	if(row.shimingzhi == 0){
                        		return '<span class="label label-danger">未认证</span>';
                        	}
                        	if(row.shimingzhi == 1){
                        		return '<span class="label label-primary">已认证</span>';
                        	}
                        }
                    },
                    {
                        field: 'credit2',
                        title: '累计余额'
                    },
                    
                    {
                        field: 'sTime',
                        title: '申请时间'
                    },
                    {
                        field: 'payStatus',
                        title: '支付状态',
                        formatter: function (value, row, index) {
                                if (value == '1') {
                                    return '<font color="red">支付中</font>'
                                }else if (value == '0') {
                                    return '<font color="red">未支付</font>'
                                }else if(value == '2'){
                                    return '<font color="red">已完成</font>'
                                }else if(value == '3'){
                                    return '<font color="red">支付失败</font>'
                                }
                        }
                    },
                    {
                        field: 'isteam',
                        title: '审核状态',
                        formatter: function (value, row, index) {
                        	if(row.isteam == 4){
                        		return '<span class="label label-primary">审核通过未开通</span>';
                        	}else{
                        		 return '<span class="label label-danger">待审核</span>';
                        	}
                         }
                    },
                    {
                        field: 'teamNum',
                        title: '门店批次',
                        formatter: function (value, row, index) {
                        	if(row.teamNum == 0){
                        		return '最新批次';
                        	}else{
                        		return row.teamNum;
                        	}
                         }
                    },
                    {
                    	field: 'id',
                        title: '操作',
                        align: 'center',
                        width: '20%',
                        formatter: function (value, row, index) {
                        	 var e = '<a style="margin-left: 20px;" class="btn btn-primary btn-sm ' + s_detail_h + '" href="#" mce_href="#" title="查看详情" onclick="detail(\''
                             + value + '\')"><i class="fa fa-edit"></i>查看详情</a> ';

	                         var f = '<a class="btn btn-success btn-sm ' + s_batch_h + '" href="#" mce_href="#" title="设置门店批次" onclick="batchEdit(\''
	                             + value + '\')">' + '<i class="fa fa-wrench"></i>设置门店批次</a> ';
	
	                         var hmdString = '';
                        	 var hmdValue = '';
                        	 if(row.status == 9) {
                        		 hmdString = '移出黑名单';
                        		 hmdValue = '1';
                        	 }else{
                        		 hmdString = '加入黑名单';
                        		 hmdValue = '9';
                        	 }
	                         
	                         var gDuo ='<div class="dropdown pull-right" style="text-align: left;position: absolute;z-index: auto;margin-top: -30px;">' +
				              			   '<button type="button" class="btn dropdown-toggle btn-primary btn-sm" id="dropdownMenu1" data-toggle="dropdown">=更多<span class="caret"></span></button>' +
				              		       '<ul class="dropdown-menu" style="left:0 !important;" role="menu" aria-labelledby="dropdownMenu1">' +
				              			      '<li role="presentation">' + 
				              			           '<a role="menuitem" tabindex="-1" href="#" onclick="hmd(\''+value+'\', ' + hmdValue + ')">' + hmdString + '</a>' + 
				              			      '</li>' + 
				              			      '<li role="presentation" class="divider"></li>' + 
				              			      '<li role="presentation">' +
				              			           '<a role="menuitem" tabindex="-1" href="#" onclick="updateBondmoney(\''+value+'\')">修改保证金</a>' + 
				              			      '</li>' + 
				              			      '<li role="presentation" class="divider"></li>' + 
				              			      '<li role="presentation">' +
				              				       '<a role="menuitem" tabindex="-1" href="#" onclick="bondFlow(\''+value+'\')">保证金流水</a>' +
				              			      '</li>' +
				              			      '<li role="presentation" class="divider"></li>' + 
				              			      '<li role="presentation">' +
				              				       '<a role="menuitem" tabindex="-1" href="#" onclick="orderList(\''+value+'\')">订单记录</a>' +
				              			      '</li>' +
				              			      '<li role="presentation" class="divider"></li>' + 
				                   			  '<li role="presentation">' +
			                 				       '<a role="menuitem" tabindex="-1" href="#" onclick="balanceFlow(\''+value+'\')">余额流水</a>' +
			                 			      '</li>' +
			                 			      '<li role="presentation" class="divider"></li>' + 
			                 			      /*'<li role="presentation">' +
			                 				       '<a role="menuitem" tabindex="-1" href="#" onclick="withdrawals(\''+value+'\')">提现记录</a>' +
			                 			      '</li>' +*/
				              		       '</ul>' +
			            	           '</div>';
	                         
	                         return e + f + gDuo;
                        }
                    }]
            });
}
function reLoad() {
    $('#noCheckCommandersTable').bootstrapTable('refresh');
}
function removeBlack(id) {
    layer.confirm('确定要移除黑名单？', {
        btn: ['确定', '取消']
    }, function () {
        $.ajax({
            url: prefix + "/removeBlack",
            type: "post",
            data: {
                'id': id
            },
            beforeSend: function (request) {
                index = layer.load();
            },
            success: function (r) {
                if (r.code == 0) {
                    layer.close(index);
                    layer.msg(r.msg);
                    reLoad();
                } else {
                    layer.msg(r.msg);
                }
            }
        });
    })
}
function batchUpdates() {
    var rows = $('#noCheckCommandersTable').bootstrapTable('getSelections'); // 返回所有选择的行，当没有选择的记录时，返回一个空数组
    if (rows.length == 0) {
        layer.msg("请选择要设置的数据");
        return;
    }
    //var ids = new Array();
    var ids = '';
    // 遍历所有选择的行数据，取每条数据对应的ID
    $.each(rows, function (i, row) {
        //ids[i] = row['id'];
        ids += row['id'] + ',';
    });
    console.log(ids);
    layer.open({
        id:'noCheck_edits',
        type : 2,
        title : '设置门店批次',
        maxmin : true,
        shadeClose : false,
        area : [ '30%', '40%' ],
        content : prefix + '/batchEdits/' + ids
    });
}

function batchExamine() {
    var rows = $('#noCheckCommandersTable').bootstrapTable('getSelections'); // 返回所有选择的行，当没有选择的记录时，返回一个空数组
	if (rows.length == 0) {
		layer.msg("请选择要审核的数据");
		return;
	}
	layer.confirm("确认要审核选中的'" + rows.length + "'条数据吗?", {
		btn : [ '确定', '取消' ]
	}, function() {
		var ids = '';
		$.each(rows, function(i, row) {
			 ids += row['id'] + ',';
		});
		$.ajax({
			type : 'POST',
			data : {
				"ids" : ids
			},
			url : prefix + '/batchExamine',
			success : function(r) {
				if (r.code == 0) {
					layer.msg(r.msg);
					reLoad();
				} else {
					layer.msg(r.msg);
				}
			}
		});
	}, function() {});
}

function add() {
    // iframe层
    layer.open({
        id:'centre_add',
        type: 2,
        title: '添加商户',
        maxmin: true,
        shadeClose: false, // 点击遮罩关闭层
        area : [ '60%', '80%' ],
        content: prefix + '/add' // iframe的url
    });
}

function batchEdit(id) {
    layer.open({
        id:'noCheck_edit',
        type : 2,
        title : '设置门店批次',
        maxmin : true,
        shadeClose : false,
        area : [ '30%', '40%' ],
        content : prefix + '/batchEdit/' + id
    });
}

function detail(id) {
    layer.open({
        id:'noCheck_detail',
        type : 2,
        title : '查看详情',
        maxmin : true,
        shadeClose : false,
        area : [ '60%', '80%' ],
        content : prefix + '/detail/' + id
    });
}

/**
 * 未审核列表导出excel
 */
function exportNocheck() {
    var startDate =  $.trim($("#startDate").val());
    var endDate =  $.trim($("#endDate").val());
    var teamNum =  $.trim($("#teamNum").val());
    var nickname =  $.trim($("#nickname").val());
    var mobile =  $.trim($("#mobile").val());
    var centreId = $.trim($("#centreId").val());
    window.location.href = prefix + "/exportNoCheck?startDate="+startDate+"&endDate="+endDate+"&teamNum="+teamNum+"&nickname="+nickname+"&mobile="+mobile+"&centreId="+centreId;
}

function orderList(id) {
	 layer.open({
	        id:'commander_orderList',
	        type : 2,
	        title : '订单列表',
	        maxmin : true,
	        shadeClose : false,
	        area : [ '90%', '80%' ],
	        content : '/member/manage/getOrderList/' + id
	    });
}

function updateBondmoney(id) {
	 layer.open({
	        id:'commander_updateBondmoney',
	        type : 2,
	        title : '修改保证金',
	        maxmin : true,
	        shadeClose : false,
	        area : [ '40%', '55%' ],
	        content : '/member/manage/updateBondmoney/' + id
	    });
}

function hmd(id, status) {
	var str = '';
	if(status == 9){
		str = '确定添加黑名单吗？';
	}else{
		str = '确定移出黑名单吗？';
	}
    layer.confirm(str, {
        btn: ['确定', '取消']
    }, function () {
        $.ajax({
            url: "/member/manage/hmd",
            type: "post",
            data: {
                'id': id,
                'status': status
            },
            beforeSend: function (request) {
                index = layer.load();
            },
            success: function (r) {
                if (r.code == 0) {
                    layer.close(index);
                    layer.msg(r.msg);
                    reLoad();
                } else {
                    layer.msg(r.msg);
                }
            }
        });
    })
}

function bondFlow(id) {
	 layer.open({
	        id:'commander_bondFlow',
	        type : 2,
	        title : '保证金流水',
	        maxmin : true,
	        shadeClose : false,
	        area : [ '60%', '80%' ],
	        content : '/flow/bond/bondFlowToMid/' + id
	    });
}

function balanceFlow(id) {
	 layer.open({
	        id:'commander_balanceFlow',
	        type : 2,
	        title : '余额流水',
	        maxmin : true,
	        shadeClose : false,
	        area : [ '60%', '80%' ],
	        content : '/flow/balance/balanceFlowToMid/' + id
	    });
}

function withdrawals(id) {
	 layer.open({
	        id:'commander_withdrawals',
	        type : 2,
	        title : '提现记录',
	        maxmin : true,
	        shadeClose : false,
	        area : [ '60%', '90%' ],
	        content : '/finance/withdrawals/withdrawalsToMid/' + id
	    });
}